package com.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.entity.EduChapter;
import com.zhao.entity.EduVideo;
import com.zhao.entity.chapter.Chapter;
import com.zhao.entity.chapter.Video;
import com.zhao.exceptionHandel.GloableException;
import com.zhao.mapper.EduChapterMapper;
import com.zhao.mapper.EduVideoMapper;
import com.zhao.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiaozhao
 * @since 2021-07-26
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduChapterMapper chapterMapper;
    @Autowired
    private EduVideoMapper videoMapper;
    @Override
    public List<Chapter> getChapterAndVideo(String courseId) {
        //根据课程id获取相应的章节信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = chapterMapper.selectList(wrapper);
        //根据课程id获取相应的小节信息
        List<EduVideo> eduVideos = videoMapper.selectList(wrapper);
        //新建一个集合存储最后的结果
        List<Chapter>characterList=new ArrayList<>();
        //遍历得到每一个一级目录
        for (EduChapter chapter: eduChapters) {
            Chapter c=new Chapter();
            //把chapter中的属性值赋值给c
            BeanUtils.copyProperties(chapter,c);
            //创建二级目录集合
            List<Video>videos=new ArrayList<>();
            //遍历得到每一个二级目录
            for(EduVideo eduVideo:eduVideos){
                Video video=new Video();
                BeanUtils.copyProperties(eduVideo,video);
                //如果一级目录的id等于二级目录的chpater_id
                if(chapter.getId().equals(eduVideo.getChapterId())){
                    //把二级目录添加进二级目录集合中
                    videos.add(video);
                }
            }
            //添加一级目录的子节点
            c.setChildren(videos);
            //把一节目录添加进集合中
            characterList.add(c);
        }
        return characterList;
    }


    @Override
    public String saveChapter(EduChapter chapter) {
        int insert = chapterMapper.insert(chapter);
        String id = chapter.getId();
        return id;
    }

    @Override
    public String updateChapter(EduChapter chapter) {
        chapterMapper.updateById(chapter);
        String id = chapter.getId();
        return id;
    }

    @Override
    public void deletechapter(String id) {
        //先根据章节信息查询出小节信息
        QueryWrapper<EduVideo> wrapper=new QueryWrapper();
        wrapper.eq("chapter_id",id);
        List<EduVideo> eduVideos = videoMapper.selectList(wrapper);
        //删除章节信息
        chapterMapper.deleteById(id);
        //批量删除小节信息
        List<String>ids=new ArrayList<>();
        for(EduVideo eduVideo:eduVideos){
            String videoId=eduVideo.getId();
            ids.add(videoId);
        }
        videoMapper.deleteBatchIds(ids);
    }
}
